

*For Margaret dropbox folders 
global NCREIF "/Users/BeckaBrolinson/Dropbox/NCREIF/data" 
global build 	"$NCREIF/build" 
global analysis "$NCREIF/analysis"
global results 	"$analysis/results"
global figures 	"$analysis/figures" 


*------------------------------------------------------------------------------*
*	Step 10- Rennovation Data	   *
*------------------------------------------------------------------------------*	


	
*Testing renovation data 

use "$build/FakeNCREIFAnnualizedDatawcontrol_postmatch2.dta", clear

	gen sqft1000s = sqft / 1000 
	gen HDD1000s = HDD / 1000 
	gen CDD1000s = CDD / 1000 
	gen used_space1000s = used_space/ 1000
	gen real_capex_ti_sqft = real_capex_ti / sqft 
	gen real_capex_bldimp_sqft = real_capex_bldimp / sqft 
	
*ssc install carryforward
*Carry forward renovation year data 
	sort propnum year
	by propnum: carryforward lastrenovatedyear, replace 

	replace lastrenovatedyear=lastrenovatedyear[_n+1] if propnum==propnum[_n+1] & lastrenovatedyear==.
	replace lastrenovatedyear=lastrenovatedyear[_n+1] if propnum==propnum[_n+1] & lastrenovatedyear==.
	replace lastrenovatedyear=lastrenovatedyear[_n+1] if propnum==propnum[_n+1] & lastrenovatedyear==.
	replace lastrenovatedyear=lastrenovatedyear[_n+1] if propnum==propnum[_n+1] & lastrenovatedyear==.
	replace lastrenovatedyear=lastrenovatedyear[_n+1] if propnum==propnum[_n+1] & lastrenovatedyear==.
	replace lastrenovatedyear=lastrenovatedyear[_n+1] if propnum==propnum[_n+1] & lastrenovatedyear==.
	replace lastrenovatedyear=lastrenovatedyear[_n+1] if propnum==propnum[_n+1] & lastrenovatedyear==.
	replace lastrenovatedyear=lastrenovatedyear[_n+1] if propnum==propnum[_n+1] & lastrenovatedyear==.
	replace lastrenovatedyear=lastrenovatedyear[_n+1] if propnum==propnum[_n+1] & lastrenovatedyear==.
	replace lastrenovatedyear=lastrenovatedyear[_n+1] if propnum==propnum[_n+1] & lastrenovatedyear==.
	replace lastrenovatedyear=lastrenovatedyear[_n+1] if propnum==propnum[_n+1] & lastrenovatedyear==.
	replace lastrenovatedyear=lastrenovatedyear[_n+1] if propnum==propnum[_n+1] & lastrenovatedyear==.
	replace lastrenovatedyear=lastrenovatedyear[_n+1] if propnum==propnum[_n+1] & lastrenovatedyear==.
	replace lastrenovatedyear=lastrenovatedyear[_n+1] if propnum==propnum[_n+1] & lastrenovatedyear==.
	replace lastrenovatedyear=lastrenovatedyear[_n+1] if propnum==propnum[_n+1] & lastrenovatedyear==.
	replace lastrenovatedyear=lastrenovatedyear[_n+1] if propnum==propnum[_n+1] & lastrenovatedyear==.

	*Test to make sure last ren year have all been filled in 
	capture drop id_unique 
	preserve 
	bysort propnum lastrenovatedyear: gen id_unique=_n==1 
	keep if id_unique==1 
	drop id_unique 
	bysort propnum: gen id_unique=_n==1 
	restore 
	*Some lastren year are listed as 0, replace these observations as missing 
	replace lastrenovatedyear=. if lastrenovatedyear==0 
	*Some of the renovated yeras are the same as year built, rpelace these as missing 
	gen cleaned_renyear= lastrenovatedyear 
	replace cleaned_renyear= . if lastrenovatedyear==yrbuilt
	*Generate a new version of year built or last ren that takes in ren year 
	gen mostrecentupdate= yrbuilt
	replace mostrecentupdate= lastrenovatedyear if lastrenovatedyear>yrbuilt & !missing(lastrenovatedyear) 
	*Generate an indicator if building is renovated between 2000-2015 
	generate d_reno = (cleaned_renyear>=2000) if !missing(cleaned_renyear)
	replace d_reno = 0 if missing(d_reno) 

	*Generate an indicator if building is built between 2000-2015 
	generate d_built = (yrbuilt>=2000) if !missing(yrbuilt)
	replace d_built=0 if missing(d_built) 
	*Generate an indicator if building is most recently updated (built or ren) between 2000-2015
	generate d_update= (mostrecentupdate>=2000 & year >= mostrecentupdate) if !missing(mostrecentupdate) 
	replace d_update=0 if missing(d_update) 
	
	
	*Generate an indicator if a building is updated/built within three years before first cert
	*First generate the difference 
	gen time_update_cert= mostrecentupdate-firstyearrated 
	generate d_cert_update = (time_update_cert>=-3 & time_update_cert<=0)
	
	gen time_ren_cert= cleaned_renyear-firstyearrated 
	generate d_cert_ren = (time_ren_cert>=-3 & time_ren_cert<=0)

	gen time_built_cert= yrbuilt - firstyearrated
	generate d_cert_built = (time_built_cert>=-3 & time_built_cert<=0)
 
 
	*Linear probability model for probability of being post-cert on being updated 
	set more off 

	#d ;
	global Cov0 	""; 
	global Cov1 	"Covered_E real_elecprice real_gasprice Unemployment HDD1000s CDD1000s 
	age2 used_space1000s
	 percentleased real_capex_ti_sqft real_capex_bldimp_sqft "; 
	global Cov2 	"Covered_E real_elecprice real_gasprice Unemployment HDD1000s CDD1000s 
	  age2 percentleased used_space1000s
	dFundType1 dFundType2 dFundType4 dFundType5 dFundType6";
	global Cov3	"Covered_E real_elecprice Unemployment HDD1000s CDD1000s 
	age2 used_space1000s"; 
	#d cr 
	
*using hdfe
	local Covariates "Cov0 Cov1 Cov2 Cov3" 
	foreach Cov of local Covariates{
	eststo LPM_treat: reghdfe  interaction d_update, absorb(i.city_cat#i.year) vce(cluster cbsa)
	estfe  LPM_treat, labels(year "Year FE" city_cat#year "City by Year FE")
	eststo LPM_treatCov1_FE: reghdfe  interaction d_update logrealutilpersf logrealrentpersf $Cov1, absorb( i.city_cat#i.year) vce(cluster cbsa)
	estfe  LPM_treatCov1_FE, labels(year "Year FE" city_cat#year "City by Year FE")
	eststo LPM_treat`Cov': reghdfe  interaction d_update logrealutilpersf logrealrentpersf $`Cov', absorb(i.propnum i.city_cat#i.year) vce(cluster cbsa)
	estfe  LPM_treat`Cov', labels(year "Year FE" city_cat#year "City by Year FE" propnum "Property FE")
	}
	
	label var interaction "Cert*Post" 
	label var treat "1[treat=1]"
	label var logrealrentpersf "ln(Rent/Sq. Ft.) (\\$)"
	label var Covered_E "Benchmarking Law" 
	label var real_elecprice "Avg. Elec. Price (\\$/MWh)" 
	label var real_gasprice "Avg. Gas Price (\\$/Mcf)" 
	label var Unemployment "Unemployment" 
	label var age "Building Age" 
	label var age2 "Building Age Squared" 
	label var used_space "Used Space (Pct. Leased * Sq. Ft.)"
	label var used_space1000s "Used Space (Pct. Leased * Sq. Ft.) (1000s)"
	labe var percentleased "Percent Leased (\%)"
	label var real_capex_ti "Cap Exp. (Tenant Imp.)" 
	label var real_capex_ti_sqft "Cap Exp./Sq. Ft. (Tenant Imp.) (\\$)"
	label var real_capex_bldimp "Cap Exp. (Bldg. Imp.)"
	label var real_capex_bldimp_sqft "Cap Exp./Sq. Ft. (Bldg. Imp.) (\\$)"
	label var d_update "1[update]"
	label var HDD "HDD" 
	label var HDD1000s "HDD (1000s)" 
	label var CDD "CDD" 
	label var CDD1000s "CDD (1000s)" 
	label var rl_yr_rentpersf "Rent per Sq. Ft."
	label var logrealutilpersf "ln(Util/Sq. Ft.) (\\$)"
	label var rl_yr_rentpersf "Rent per Sq. Ft."
	label var rl_yr_utilpersf "Util. per Sq. Ft."
	label var yrbuilt "Year Built" 
	label var sqft "Square Feet"
	label var sqft1000s "Square Feet (1000s)" 
	
	*make Latex Table 
	esttab LPM_treat LPM_treatCov1_FE LPM_treatCov1 LPM_treatCov3 using "$results/09_lpm_renovations_matched.tex" , label replace booktabs ///
	alignment(SSSS) ///
	b(%12.3f) se(%12.3f) star(* 0.05) /// sets format of parameters, standard errors, and stars
	stats(N, fmt(%9.0fc)) /// adds comma to Observation number
	nonotes nogaps /// removes notes from bottom of table 
	indicate(`r(indicate_fe)', label("Y" "")) /// adds y & N for FE inclusions
	title(Matched Sample: Linear Probability Model of Updates on Probability of Certification \label{tabappendix04lpmrenovations}) ///
	addnote({\scriptsize * p$<$0.05. The standard errors reported in parenthesis have been clustered at the CBSA level.})

	
